# -*- coding: utf-8 -*-
from multiprocessing import Pool

import requests

import json, csv
from collections.abc import Iterable
import random, time
import importlib, sys
import queue
import sys, io

from fake_useragent import UserAgent

sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='gbk')
import urllib3
import logging

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)  # 去掉请求警告
from save_mysql import mysql_checking
from save_mysql import save_in_mysql

ua = [
    'Mozilla/5.0 (Linux; Android 8.1.0; JSN-AL00 Build/HONORJSN-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/66.0.3359.126 MQQBrowser/6.2 TBS/045018 Mobile Safari/537.36 MMWEBID/3567 MicroMessenger/7.0.10.1580(0x27000AFE) Process/tools NetType/WIFI Language/zh_CN ABI/arm64',
    'Mozilla/5.0 (Linux; Android 9; RMX1971 Build/PKQ1.190101.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/76.0.3809.89 Mobile Safari/537.36 T7/11.19 SP-engine/2.15.0 baiduboxapp/11.19.5.10 (Baidu; P1 9)',
    'Mozilla/5.0 (Linux; Android 9; MIX 2 Build/PKQ1.190118.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/66.0.3359.126 MQQBrowser/6.2 TBS/045111 Mobile Safari/537.36 MMWEBID/8679 MicroMessenger/7.0.10.1580(0x27000AFF) Process/tools NetType/WIFI Language/zh_CN ABI/arm64',
    'Mozilla/5.0 (Linux; Android 10; LIO-AN00 Build/HUAWEILIO-AN00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/76.0.3809.89 Mobile Safari/537.36 T7/11.19 SP-engine/2.15.0 baiduboxapp/11.19.5.10 (Baidu; P1 10) NABar/1.0',
    'Mozilla/5.0 (Linux; U; Android 9; zh-CN; RVL-AL09 Build/HUAWEIRVL-AL09) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.108 UCBrowser/12.8.5.1065 Mobile Safari/537.36',
    'Mozilla/5.0 (Linux; U; Android 9; zh-CN; Redmi 6 Pro Build/PKQ1.180917.001) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.108 UCBrowser/12.3.6.1016 Mobile Safari/537.36',
    'Mozilla/5.0 (Linux; U; Android 9; zh-CN; Redmi Note 8 Pro Build/PPR1.180610.011) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.108 UCBrowser/12.8.5.1065 Mobile Safari/537.36',
    'Mozilla/5.0 (Linux; U; Android 9; zh-cn; MI 6X Build/PKQ1.180904.001) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/71.0.3578.141 Mobile Safari/537.36 XiaoMi/MiuiBrowser/11.7.34',
    'Mozilla/5.0 (Linux; Android 9; JAT-AL00 Build/HONORJAT-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/76.0.3809.89 Mobile Safari/537.36 T7/11.19 SP-engine/2.15.0 baiduboxapp/11.19.5.10 (Baidu; P1 9) NABar/1.0',
    'Mozilla/5.0 (Linux; Android 9; BLA-AL00 Build/HUAWEIBLA-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/66.0.3359.126 MQQBrowser/6.2 TBS/045111 Mobile Safari/537.36 MMWEBID/9018 MicroMessenger/7.0.10.1580(0x27000AFF) Process/tools NetType/4G Language/zh_CN ABI/arm64',
    'Mozilla/5.0 (Linux; Android 9; ONEPLUS A5010 Build/PKQ1.180716.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/66.0.3359.126 MQQBrowser/6.2 TBS/045114 Mobile Safari/537.36',
    'Mozilla/5.0 (Linux; U; Android 9; zh-cn; MI 9 SE Build/PKQ1.181121.001) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/71.0.3578.141 Mobile Safari/537.36 XiaoMi/MiuiBrowser/11.7.34',
    'Mozilla/5.0 (Linux; Android 10; TNY-AL00 Build/HUAWEITNY-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/66.0.3359.126 MQQBrowser/6.2 TBS/45016 Mobile Safari/537.36 MMWEBID/2551 MicroMessenger/7.0.10.1580(0x27000AFE) Process/tools NetType/WIFI Language/zh_CN ABI/arm64',
    'Mozilla/5.0 (Linux; U; Android 10; zh-cn; TNY-AL00 Build/HUAWEITNY-AL00) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/66.0.3359.126 MQQBrowser/10.0 Mobile Safari/537.36',
    'Mozilla/5.0 (Linux; Android 9; JKM-AL00 Build/HUAWEIJKM-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/76.0.3809.89 Mobile Safari/537.36 T7/11.19 SP-engine/2.15.0 baiduboxapp/11.19.5.10 (Baidu; P1 9)',
    'Mozilla/5.0 (Linux; Android 9; Lenovo L38111 Build/PKQ1.190127.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/76.0.3809.89 Mobile Safari/537.36 T7/11.17 SP-engine/2.13.0 baiduboxapp/11.17.0.13 (Baidu; P1 9)',
    'Mozilla/5.0 (Linux; Android 9; vivo X21 Build/PKQ1.180819.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/76.0.3809.89 Mobile Safari/537.36 T7/11.19 SP-engine/2.15.0 baiduboxapp/11.19.5.10 (Baidu; P1 9)',
    'Mozilla/5.0 (Linux; Android 10; ELE-AL00 Build/HUAWEIELE-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/66.0.3359.126 MQQBrowser/6.2 TBS/045104 Mobile Safari/537.36 MMWEBID/6317 MicroMessenger/7.0.10.1580(0x27000AFC) Process/tools NetType/WIFI Language/zh_CN ABI/arm64',
    'Mozilla/5.0 (Linux; U; Android 10; zh-cn; HMA-TL00 Build/HUAWEIHMA-TL00) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/66.0.3359.126 MQQBrowser/10.1 Mobile Safari/537.36',
    'Mozilla/5.0 (Linux; Android 9; Redmi Note 8 Pro Build/PPR1.180610.011; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/63.0.3239.83 Mobile Safari/537.36 T7/11.16 lite baiduboxapp/4.11.5.11 (Baidu; P1 9)',
    'Mozilla/5.0 (Linux; U; Android 9; zh-cn; PCGM00 Build/PKQ1.190519.001) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/66.0.3359.126 MQQBrowser/10.1 Mobile Safari/537.36',
    'Mozilla/5.0 (Linux; Android 9; JKM-AL00 Build/HUAWEIJKM-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/66.0.3359.126 MQQBrowser/6.2 TBS/045118 Mobile Safari/537.36 MMWEBID/2787 MicroMessenger/7.0.10.1580(0x27000AFC) Process/tools NetType/WIFI Language/zh_CN ABI/arm64',
    'Mozilla/5.0 (Linux; U; Android 10; zh-CN; ONEPLUS A6000 Build/QKQ1.190716.003) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.108 UCBrowser/12.10.0.1163 UCTurbo/1.9.3.900 Mobile Safari/537.36',
    'Mozilla/5.0 (Linux; U; Android 10; zh-CN; VOG-AL10 Build/HUAWEIVOG-AL10) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.108 UCBrowser/12.8.5.1065 Mobile Safari/537.36',
    'Mozilla/5.0 (Linux; U; Android 9; zh-cn; SM-G9650 Build/PPR1.180610.011) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/66.0.3359.126 MQQBrowser/10.0 Mobile Safari/537.36',
    'Mozilla/5.0 (Linux; Android 10; JSN-AL00 Build/HONORJSN-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/67.0.3396.87 XWEB/1168 MMWEBSDK/191201 Mobile Safari/537.36 MMWEBID/6118 MicroMessenger/7.0.10.1580(0x27000AFE) Process/toolsmp NetType/4G Language/zh_CN ABI/arm64',
    'Mozilla/5.0 (Linux; Android 10; VOG-TL00 Build/HUAWEIVOG-TL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/66.0.3359.126 MQQBrowser/6.2 TBS/45016 Mobile Safari/537.36 MMWEBID/8723 MicroMessenger/7.0.10.1580(0x27000AFC) Process/tools NetType/4G Language/zh_CN ABI/arm64',
    'Mozilla/5.0 (Linux; U; Android 9; zh-cn; JSN-AL00a Build/HONORJSN-AL00a) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/66.0.3359.126 MQQBrowser/10.1 Mobile Safari/537.36',
    'Mozilla/5.0 (Linux; Android 10; ELE-AL00 Build/HUAWEIELE-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/67.0.3396.87 XWEB/1168 MMWEBSDK/191201 Mobile Safari/537.36 MMWEBID/6553 MicroMessenger/7.0.10.1580(0x27000AFC) Process/toolsmp NetType/WIFI Language/zh_CN ABI/arm64',
    'Mozilla/5.0 (Linux; Android 9; V1829A Build/PKQ1.181030.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/66.0.3359.126 MQQBrowser/6.2 TBS/045105 Mobile Safari/537.36 MMWEBID/1118 MicroMessenger/7.0.10.1580(0x27000AFD) Process/tools NetType/4G Language/zh_CN ABI/arm64',
    'Mozilla/5.0 (Linux; Android 9; Note9 Build/PKQ1.181203.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/63.0.3239.83 Mobile Safari/537.36 T7/11.15 baiduboxapp/11.15.5.10 (Baidu; P1 9)',
]
# # 隧道域名:端口号
# tunnel = "tps198.kdlapi.com:15818"
# # 用户名密码方式
# username = "t11199539405296"
# password = "8maa29md"
# proxies = {
#     "http": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": tunnel},
#     "https": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": tunnel}
# }

# q = queue.Queue()


# logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(lineno)d - %(message)s',
# filemode='a') # ,filename='test.log'
# logger = logging.getLogger(__name__)

save_dict = {
    'companyName': '',  # 公司名称
    'legalPerson': '',  # 法人
    'registerDate': '',  # 注册时间
    'address': '',  # 地址
    'registerCapital': '',  # 注册资金
    'staffSize': '',  # 人员规模
    'tags': '',  # 企业类型
    'bizStatus': '',  # 营业状态
    'industry': '',  # 行业类
    'opScope': '',  # 经营范围

    'business_capacit': '',  # 经营实力
    'innovation': '',  # 创新评分
    'litigation_risk': '',  # 诉讼风险
    'operational_risk': '',  # 运营风险
    'shareholders': '',  # 股东人员
    'manager_list': '',  # 高管人员
    'entBrief': '',  # 简介
    'ocid': '',
    'phoneList': '',
    'emailList': '',
    'mobile_no': '',

    'management_info': '',  # 工商信息
    'company_report': '',  # 企业年报
    'change_infor': '',  # 更改信息
    'foreign_investment': '',  # 对外投资
    'branch': '',  # 分支机构
    'core_team': '',  # 核心团队

    'property': '',  # 知识产权
    'company_development': '',  # 企业发展
    'business_info': '',  # 经营信息
    'legal_risk': '',  # 司法风险
    'management_risk': '',  # 经营风险
}

headers = {
    'Content-Type': 'application/json;charset=UTF-8',
    # 'Host': 'holmes.taobao.com',
    'User-agent': random.choice(ua),
    'Connection': 'close',
    'X-XSRF-TOKEN': '44990af6-5c83-4752-a714-cf438ae6be3f',
    'deviceId': '8f6e3733ca6af1d918de5b31f7cb12',
    'authcode': 'f0ba258b2ac5356b8b392bb9dfd40359',
    # "connection":"close"
}
PROXY_POOL_URL = 'http://localhost:5555/random'


def get_proxy():
    try:
        response = requests.get(PROXY_POOL_URL)
        if response.status_code == 200:
            return response.text
    except ConnectionError:
        return None


#     'http': get_proxy()
# }
# proxie = {

# print(headers)


def company_josn(companyName, phone, city):
    url = 'https://holmes.taobao.com/ding/corpquery/company/category2V3'
    data = {
        "companyName": companyName,
        "deviceId": "a9eae24bba847a2616c9a87862df96bb",
        "authCode": '',
        "platform": "tb"}
    data_all = post_requests(url, data)
    print(data_all)
    if data_all and data_all.get('data'):
        try:
            save_dict['companyName'] = data_all['data']['summary'].get('companyName')
        except:
            save_dict['companyName'] = data_all['data']['summary'].get('legalPerson')
        legalPerson = data_all['data']['summary'].get('legalPerson')
        if legalPerson == None:
            legalPerson = companyName
        save_dict['legalPerson'] = legalPerson
        save_dict['registerDate'] = data_all['data']['summary'].get('registerDate')
        save_dict['address'] = data_all['data']['summary'].get('address')
        save_dict['registerCapital'] = data_all['data']['summary'].get('registerCapital')
        save_dict['staffSize'] = str(data_all['data']['summary'].get('staffSize')) + '人'

        tags = data_all['data']['summary'].get('tags')
        if tags:
            save_dict['tags'] = str(tags)
        else:
            save_dict['tags'] = ''
        # print(companyName)汕尾市城区大地照相馆
        save_dict['bizStatus'] = data_all['data']['summary']['bizStatus']

        # 行业类
        save_dict['industry'] = data_all['data']['summary']['industryTwo']

        # 经营范围
        save_dict['opScope'] = data_all['data']['summary']['opScope']

        identityId = data_all['data']['summary']['identityId']

        ocid = data_all['data']['summary']['ocid']
        save_dict['ocid'] = ocid

        phoneList = data_all['data']['summary']['phoneList']
        if phoneList == None:
            phoneList = phone
        else:
            if phone:
                phoneList.append(phone)

        save_dict['phoneList'] = str(phoneList).replace('[', '').replace(']', '').replace('\'', '')
        mobile_no = [phone for phone in phoneList if len(phone) == 11] if isinstance(phoneList,
                                                                                     Iterable) == True else ''

        if len(mobile_no) < 1 and len(phoneList) == 11:
            mobile_no = phoneList

        print(phone, city)

        if mobile_no:
            if mysql_checking(mobile_no[0]) == True:

                save_dict['mobile_no'] = mobile_no[0]
                # 同行对比
                company_contrast(ocid)

                # 股东人员
                save_dict['shareholders'] = str(get_shareholders(ocid)).replace('[', '').replace(']', '').replace('\'',
                                                                                                                  '').replace(
                    '"', '')

                # 高管人员
                save_dict['manager_list'] = str(get_manager(ocid)).replace('[', '').replace(']', '').replace('\'',
                                                                                                             '').replace(
                    '"', '')

                # 简介
                save_dict['entBrief'] = data_all['data']['summary']['entBrief']

                save_dict['emailList'] = str(data_all['data']['summary']['emailList']).replace('[', '').replace(']',
                                                                                                                '').replace(
                    '\'', '')
                try:
                    save_in_mysql(save_dict)
                except Exception as e:
                    logging.info(e)

                # 公司信息
                # print('公司信息',data_all['data']['category'])
                company_category(data_all['data']['category'], ocid)

        else:
            print('没有手机号')

        print('\n')


# 获取高管人员
def get_manager(ocid):
    url = 'https://holmes.taobao.com/ding/corpquery/company/mainPersons'
    data = {"ocid": ocid, "deviceId": "", "authCode": "", "platform": "tb"}
    manager_data = post_requests(url, data)
    manager_list = []
    if manager_data and isinstance(manager_data['data'], Iterable) == True:
        for each in manager_data['data']:
            name = each['name']
            id = each['id']
            tags = each['tags']
            relateCompanyNum = each['relateCompanyNum']
            if relateCompanyNum:
                manager_txt = name + '(' + str(tags).replace('[', '').replace(']', '').replace('"',
                                                                                               '') + '：' + '关联企业' + str(
                    relateCompanyNum) + ')'
                manager_list.append(manager_txt)
        return manager_list


# 获取股东人员
def get_shareholders(ocid):
    url = 'https://holmes.taobao.com/ding/corpquery/company/stockholders'
    data = {"ocid": ocid, "deviceId": "", "authCode": "", "platform": "tb"}
    shareholders_data = post_requests(url, data)
    shareholders = []
    if shareholders_data and isinstance(shareholders_data['data'], Iterable) == True:
        for each in shareholders_data['data']:
            name = each['name']
            id = each['id']
            shareHold = each['shareHold']
            shareholders_txt = name + '(' + shareHold + ')'
            shareholders.append(shareholders_txt)
        return str(shareholders)


# 同行对比
def company_contrast(ocid):
    # 同行对比
    url = 'https://holmes.taobao.com/ding/corpquery/company/sameIndustryCompare/detail'
    data = {"ocid": ocid,
            "deviceId": "a9eae24bba847a2616c9a87862df96bb",
            "authCode": "",
            "platform": "tb"}
    # try:
    contrast_data = post_requests(url, data)
    print(contrast_data)
    if contrast_data and isinstance(contrast_data.get('data'), Iterable) == True:
        for items in contrast_data['data']:
            try:
                txt_item = '全国:' + items['countryItem']['tips'] + '；对比本省:' + items['provinceItem']['tips']
            except:
                txt_item = '暂无数据'

            if items['name'] == '经营实力':
                save_dict['business_capacit'] = txt_item
            if items['name'] == '创新评分':
                save_dict['innovation'] = txt_item
            if items['name'] == '诉讼风险':
                save_dict['litigation_risk'] = txt_item
            if items['name'] == '运营风险':
                save_dict['operational_risk'] = txt_item
    # except Exception as e:
    #     logging.info(e)


# 企业年报
def get_company_report(ocid):
    # 企业年报
    url = 'https://holmes.taobao.com/ding/detail/companyDetailCard'
    data = {"pageNo": 1, "dataModuleId": 10,
            "params": [{"key": "onecomp_id", "value": ocid}],
            "pageSize": 20, "deviceId": "a9eae24bba847a2616c9a87862df96bb",
            "authCode": "",
            "platform": "tb"}
    # try:
    report_data = post_requests(url, data)

    if report_data and report_data.get('data'):
        dataModuleId = report_data['data']['nav'].get('dataModuleId')

        company_report_list = []
        if isinstance(report_data['data']['rows'], Iterable) == True:

            value_0 = report_data['data']['rows'][0][0]
            value_1 = report_data['data']['rows'][0][1]
            if dataModuleId:
                company_report = report_data['data']['rows'][0][2] + str(
                    get_company_report_detail(dataModuleId, value_0, value_1))
                company_report_list.append(company_report)
        # except Exception as e:
        #     company_report_list = ''
        #     logging.info(e)

        return company_report_list


# 获取企业年报详情
def get_company_report_detail(dataModuleId, value_0, value_1):
    # 获取企业年报
    url = 'https://holmes.taobao.com/ding/detail/companyDetailCard'
    data = {"pageNo": 1, "dataModuleId": dataModuleId,
            "params": [{"key": "onecomp_id", "value": value_0}
                , {"key": "md5", "value": value_1}],
            "pageSize": 20, "deviceId": "a9eae24bba847a2616c9a87862df96bb",
            "authCode": "", "platform": "tb"}

    report_data = post_requests(url, data)
    if report_data and report_data.get('data'):
        company_report_txt = []
        for i, items in enumerate(report_data['data']['fields']):
            for each in report_data['data']['rows']:
                each_str = items['title'] + ':' + str(each[i])
                company_report_txt.append(str(each_str))
        return company_report_txt


# 工商信息
def business_info(ocid):
    # 工商信息
    url = 'https://holmes.taobao.com/ding/detail/companyDetailCard'
    data = {"pageNo": 1, "dataModuleId": 5,
            "params": [{"key": "onecomp_id", "value": ocid}],
            "pageSize": 20, "deviceId": "",
            "authCode": "", "platform": "tb"}

    business_data = post_requests(url, data)
    business_txt_list = []
    if business_data and business_data.get('data'):
        if isinstance(business_data['data']['fields'], Iterable) == True:
            for i, items in enumerate(business_data['data']['fields']):
                for each in business_data['data']['rows']:
                    each_str = items['title'] + ':' + str(each[i])
                    business_txt_list.append(str(each_str))
            return business_txt_list


def post_dataModuleId(ocid, dataModuleId):
    url = 'https://holmes.taobao.com/ding/detail/companyDetailCard'
    data = {"pageNo": 1, "dataModuleId": dataModuleId,
            "params": [{"key": "onecomp_id", "value": ocid}],
            "pageSize": 20, "deviceId": "",
            "authCode": "", "platform": "tb"}
    try:
        req = requests.post(url, headers=headers, verify=False, data=json.dumps(data), timeout=30)  # ,proxies=proxies
    except:
        time.sleep(2)
        req = requests.post(url, headers=headers, verify=False, data=json.dumps(data), timeout=30)  # ,proxies=proxies
    data_all = req.json()
    items_list = []
    if isinstance(data_all['data']['rows'], Iterable) == True:

        for items in data_all['data']['rows']:
            try:
                for i, each in enumerate(items):
                    each_str = data_all['data']['fields'][i]['title'] + ':' + str(each)
                    items_list.append(str(each_str))
            except:
                continue
        return items_list
    elif isinstance(data_all['data'].get('fields'), Iterable) == True:

        for items in data_all['data']:
            try:
                for i, each in enumerate(items):
                    each_str = data_all['data']['fields'][i].get('title') + ':' + str(each)
                    items_list.append(str(each_str))
            except:
                continue
        return items_list
    else:
        items_list = ''
        # print(e)
        return items_list


def company_category(basic_info, ocid):
    # 遍历模块： 基本信息；司法风险；司法风险；经营信息；知识产权；企业发展
    for items in basic_info:

        # 模块存在信息
        if items['name'] == '基本信息':
            for item in items['children']:
                if item['name'] == '工商信息':
                    save_dict['management_info'] = '工商信息' + str(business_info(ocid))

                if item['name'] == '企业年报' and item['statCount'] != 0:
                    save_dict['company_report'] = str(get_company_report(ocid))

                if item['name'] == '变更信息':
                    if item.get('statCount') != 0:
                        change_infor = post_dataModuleId(ocid, item['dataModuleId'])
                    else:
                        change_infor = ''
                    save_dict['change_infor'] = str(item['statCount']) + str(change_infor)

                if item['name'] == '对外投资':
                    if item.get('statCount') != 0:
                        foreign_investment_infor = post_dataModuleId(ocid, item['dataModuleId'])
                    else:
                        foreign_investment_infor = ''
                    save_dict['foreign_investment'] = str(item['statCount']) + '个' + str(foreign_investment_infor)

                if item['name'] == '分支机构':
                    if item.get('statCount') != 0:
                        branch_infor = post_dataModuleId(ocid, item['dataModuleId'])
                    else:
                        branch_infor = ''
                    save_dict['branch'] = str(item['statCount']) + str(branch_infor)

                if item['name'] == '核心团队':
                    if item.get('statCount') != 0:
                        core_team_infor = post_dataModuleId(ocid, item['dataModuleId'])
                    else:
                        core_team_infor = ''
                    save_dict['core_team'] = str(item['statCount']) + str(core_team_infor)

        if items['name'] == '知识产权':
            property_list = []
            if items.get('statCount') != 0:
                for each in items['children']:
                    if each['statCount'] != 0 and each.get('dataModuleId'):
                        property_list.append(each['name'] + str(post_dataModuleId(ocid, each['dataModuleId'])))
                    else:
                        property_list.append(each['name'] + str(each['statCount']))

            save_dict['property'] = str(items['statCount']) + '个' + str(property_list)

        if items['name'] == '经营信息':
            business_list = []
            if items['statCount'] != 0:
                for each in items['children']:
                    business_list.append(each.get('name') + str(each.get('statCount')))
            save_dict['business_info'] = str(items['statCount']) + '个' + str(business_list)

        if items['name'] == '司法风险':
            legal_list = []
            if items['statCount'] != 0:
                for each in items['children']:
                    if each['statCount'] != 0 and each.get('dataModuleId'):
                        legal_list.append(each['name'] + str(post_dataModuleId(ocid, each['dataModuleId'])))
                    else:
                        legal_list.append(each['name'] + str(each['statCount']))
            save_dict['legal_risk'] = str(items.get('statCount')) + '个' + str(legal_list)

        if items['name'] == '经营风险':
            management_list = []
            if items['statCount'] != 0:
                for each in items['children']:
                    management_list.append(each['name'] + str(each['statCount']))
            save_dict['management_risk'] = str(items['statCount']) + '个' + str(management_list)

        if items['name'] == '企业发展':
            company_development_list = []
            if items['statCount'] != 0:
                for each in items['children']:
                    company_development_list.append(each['name'] + str(each['statCount']))
            save_dict['company_development'] = str(items['statCount']) + '个' + str(company_development_list)


def post_requests(url, data):
    # try:
    req = requests.post(url, headers=headers, verify=False, data=json.dumps(data), timeout=30)  # ,proxies=proxies
    print(req.text)
    data_all = req.json()

    if isinstance(data_all['data'], Iterable) == True:
        return data_all
    else:
        print('重试2')
        time.sleep(random.uniform(1, 3))
        req = requests.post(url, headers=headers, verify=False, data=json.dumps(data),
                            timeout=30)  # ,proxies=proxies
        data_all = req.json()
        return data_all
    # except Exception as e:
    #     print(e,'++',data)
    #     return ''


biaoji = 0


def next_page(total, city):
    global biaoji
    url = 'https://holmes.taobao.com/ding/corp/customer/searchWithSummary'
    for next_page in range(total // 50 + 1):
        if total < int(1):
            break
        # if next_page>50:
        #     break
        next_page += 1
        print('{}第{}页'.format(city, next_page))
        data2 = {
            "region": {"area": city},
            "hasConcatWay": ["有联系电话"], "keyword": "贸易", "pageNo": next_page, "pageSize": 50,
            "orderByType": 5, "deviceId": '', "authCode": '', "platform": "tb"}

        # try:
        data = post_requests(url, data2)

        # except:
        #     print('使用代理')
        #     time.sleep(5)
        #     req = requests.post(url, headers=headers, verify=False, data=json.dumps(data2), timeout=20)#,proxies=proxies
        #     data = req.json()
        if data and data['data'].get('data'):
            try:
                data_json = data['data']['data']
                get_companyName(data_json, city)
            except:
                continue
        else:
            biaoji += 1
            print('没有数据')
            break
        if biaoji == 3:
            biaoji = 0
            break
        # time.sleep(random.uniform(0.5, 2))


def get_companyName(data_json, city):
    for i, data in enumerate(data_json):
        companyName = data['companyName']
        phone = data.get('phone')
        print(companyName)
        # try:
        company_josn(companyName, phone, city)
        # except Exception as e:
        #     logging.info(e)
        #
        #     continue


def po(g):
    city = g.strip()
    url = 'https://holmes.taobao.com/ding/corp/customer/searchWithSummary'
    data = {
        "region": {"area": city},
        "hasConcatWay": ["有联系电话"],
        "keyword": "贸易",
        "pageNo": 1, "pageSize": 50,
        "orderByType": 5, "deviceId": '', "authCode": '', "platform": "tb",
    }
    data_all = post_requests(url, data)
    if data_all and data_all.get('data'):
        try:
            print(city, data_all)
        except:
            print('shibai')
        total = data_all['data'].get('total')
        next_page(total, city)


def search_list():
    with open('citys.txt', encoding='utf-8') as f:
        citys = f.readlines()
    # pool = Pool(2)
    for city in citys[2014:2024]:
        po(city)
    #     pool.apply_async(po, (city,))
    # pool.close()
    # pool.join()


# 和平区
if __name__ == '__main__':
    # print(3333)
    search_list()
